<?php
require_once("ITransformer.php");

/**
 * Transforms the given EmbraceModel into PHP abstract class.
 * 
 * @author s.marletta
 * @version $Id: BasicTransformer.php 6 2009-04-28 20:11:07Z embrace.toolkit $
 */
class BasicTransformer implements ITransformer {
	/**
	 * Contains the path usedfor interfaces.
	 * 
	 * @var String
	 */
	private $path;
	
	/**
	 * Filename of the interface result.
	 * @var String
	 */
	private $fileName;
	
	/**
	 * Basics class name for code.
	 * @var String
	 */
	private $basicName;
	
	/**
	 * Name of the extended abstract class.
	 * @var String
	 */
	private $abstractName;
	
	/**
	 * the used model.
	 * @var EmbraceModel
	 */
	private $data;
	
	/**
	 * (non-PHPdoc)
	 * @see embrace/lib/ITransformer#transform($xml)
	 */
	public function transform($data) {
		if($data == null || empty($data))
			throw new Exception("Model must be given.");
		
		$this->data = $data;
		$this->path = "output" . $this->data->getSettings()->getSetting("service_path") . 
			"/php";
		
		if(!mkdirRecursive($this->path, 0777))
			throw new Exception("Unable to create abstract class directory.");
		
		$this->abstractName = "Abstract" . $this->data->getName();
		$this->basicName = "Basic" . $this->data->getName(); 
		$this->fileName = $this->basicName . ".php";
		
		$fHandle = fopen($this->path . "/" . $this->fileName, "w+");
		
		if(!is_resource($fHandle))
			throw new Exception("Unable to create abstract class file.");
		
		$this->writeFileComment($fHandle);
		$this->writeAbstractDescriptor($fHandle);
	
		$buffer = "}" . self::delimiter;
		$buffer .= "?>" . self::delimiter;
		fwrite($fHandle, $buffer);
	}
	
	/**
	 * Writes the interface descriptor.
	 * 
	 * @param $fHandle
	 * @return unknown_type
	 */
	protected function writeAbstractDescriptor($fHandle) {
		$buffer = "class " . $this->basicName . " extends " . 
			$this->abstractName . " {" . self::delimiter;
		
		$buffer .= self::delimiter;
		
		$arrayMethods = $this->data->getMethods();
		
		if($arrayMethods === false)
			throw new Exception("No methods contained.");
		
		foreach($arrayMethods as $method) {
			$buffer .= "\t/**" . self::delimiter;
			$buffer .= "\t * From model generated method." . self::delimiter;
			$buffer .= "\t * Type: " . $method->getMethodType() . self::delimiter;
			$buffer .= "\t */" . self::delimiter;
			$buffer .= "\tpublic function " . $method->getMethodName() . "() {" . self::delimiter;
			$buffer .= "\t\t // @todo Implement here your user method." . self::delimiter;
			$buffer .= "\t}" . self::delimiter;
			$buffer .= self::delimiter;
		}
		
		fwrite($fHandle, $buffer);
	}
	
	/**
	 * Writes the class comment to the file output.
	 * 
	 * @param $fHandle
	 */
	protected function writeFileComment($fHandle) {
		$buffer = "<?php" . self::delimiter;
		$buffer .= "require_once(\"" . $this->abstractName . ".php\");" . self::delimiter;
		$buffer .= "/**" . self::delimiter;
		$buffer .= " * " . $this->basicName . self::delimiter;
		$buffer .= " *" . self::delimiter;
		$buffer .= " * This basic class is created by using XML data model description" . self::delimiter;
		$buffer .= " * and compiled by Embrace Modelling for PHP/JS." . self::delimiter;
		$buffer .= " *" . self::delimiter;
		$buffer .= " * In this file you have to implement defined methods." . self::delimiter;
		$buffer .= " * If file will be recompiled with Embrace all changes will be loosed." . self::delimiter;
		$buffer .= " *" . self::delimiter;
		$buffer .= " * @author Embrace Modelling for PHP/JS" . self::delimiter;
		$buffer .= " * @version \$Id: BasicTransformer.php 6 2009-04-28 20:11:07Z embrace.toolkit $" . self::delimiter;
		$buffer .= "*/" . self::delimiter;
				
		fwrite($fHandle, $buffer);
	}
	
	/**
	 * 
	 * @return unknown_type
	 */
	public function createReport() {
		
	}
}
?>